/*
 * Copyright (c) 2010-2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.pivot;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.HashMap;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Map;
import org.apache.pivot.json.JSON;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ListButton;
import org.apache.pivot.wtk.PushButton;
import org.apache.pivot.wtk.TableView;

import java.io.IOException;
import java.util.Properties;

/**
 * Mew Pivot Raid Buff/Debuff specific UI components.
 *
 * @author Yawning <yawninglol at gmail.com>
 */
public class MewPivotBuffsDebuffs {
	private Component mComponent;

	private List<String> mSelfBuffs;
	private List<String> mSelfDebuffs;

	@BXML private ListButton presetButton = null;
	@BXML private PushButton presetApplyButton = null;

	@BXML private TableView buffTable = null;
	@BXML private TableView debuffTable = null;

	public MewPivotBuffsDebuffs(List<String> selfBuffs, List<String> selfDebuffs) {
		BXMLSerializer bxmlSerializer = new BXMLSerializer();

		try {
			mComponent = (Component) bxmlSerializer.readObject(getClass().getResource("MewPivotBuffsDebuffs.bxml"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SerializationException e) {
			e.printStackTrace();
		}
		bxmlSerializer.bind(this);

		buffTable.getComponentMouseButtonListeners().add(new MewTableViewMouseButtonListener());
		debuffTable.getComponentMouseButtonListeners().add(new MewTableViewMouseButtonListener());

		mSelfBuffs = selfBuffs;
		mSelfDebuffs = selfDebuffs;

		presetButton.setSelectedIndex(1);
		presetApplyButton.getButtonPressListeners().add(new ButtonPressListener() {
			@Override
			public void buttonPressed(Button button) {
				onApply();
			}
		});

		onApply();
	}

	public Component getComponent() {
		return mComponent;
	}

	@SuppressWarnings("unchecked")
	private void onApply() {
		for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData())
			JSON.put(row, "enabled", false);
		for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData())
			JSON.put(row, "enabled", false);

		if (presetButton.getSelectedItem().equals("Raid")) {
			for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData()) {
				if (row.containsKey("raidDebuff") && (Boolean) JSON.get(row, "raidDebuff") == true)
					JSON.put(row, "enabled", true);
			}

			for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData()) {
				if (row.containsKey("raidBuff") && (Boolean) JSON.get(row, "raidBuff") == true)
					JSON.put(row, "enabled", true);
			}
		} else if (presetButton.getSelectedItem().equals("Self")) {
			for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData()) {
				if (mSelfDebuffs.indexOf((String) row.get("prop")) != -1) {
					JSON.put(row, "enabled", true);
				}
			}
			for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData()) {
				if (mSelfBuffs.indexOf((String) row.get("prop")) != -1) {
					JSON.put(row, "enabled", true);
				}
			}
		}

		// Force update the display.
		List<Map<String,Object>> data = (List<Map<String,Object>>) buffTable.getTableData();
		for (int i = 0; i < buffTable.getTableData().getLength(); i++) {
			Map<String,Object> row = (Map<String, Object>) buffTable.getTableData().get(i);
			data.update(i, row);
		}

		data = (List<Map<String,Object>>) debuffTable.getTableData();
		for (int i = 0; i < debuffTable.getTableData().getLength(); i++) {
			Map<String,Object> row = (Map<String, Object>) debuffTable.getTableData().get(i);
			data.update(i, row);
		}
	}

	@SuppressWarnings("unchecked")
	public Properties toProperties() {
		Properties rval = new Properties();

		for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData()) {
			String key = JSON.get(row, "prop");
			boolean value = (Boolean) JSON.get(row, "enabled");

			rval.setProperty(key, Boolean.toString(value));
		}

		for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData()) {
			String key = JSON.get(row, "prop");
			boolean value = (Boolean) JSON.get(row, "enabled");

			rval.setProperty(key, Boolean.toString(value));
		}

		return rval;
	}

	@SuppressWarnings("unchecked")
	public void onLoad(Map<String,Object> backingStore) {
		Map<String,?> tmp;

		tmp = JSON.get(backingStore, "debuffs");
		if (tmp != null) {
			for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData()) {
				String key = JSON.get(row, "prop");

				if (tmp.containsKey(key)) {
					boolean enabled = (Boolean) tmp.get(key);
					JSON.put(row, "enabled", enabled);
				}
			}
		}

		tmp = JSON.get(backingStore, "buffs");
		if (tmp != null) {
			for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData()) {
				String key = JSON.get(row, "prop");

				if (tmp.containsKey(key)) {
					boolean enabled = (Boolean) tmp.get(key);
					JSON.put(row, "enabled", enabled);
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void onSave(Map<String,Object> backingStore) {
		HashMap<String,Object> tmp;

		tmp = new HashMap<String,Object>();
		for (Map<String,Object> row : (List<Map<String,Object>>) debuffTable.getTableData()) {
			String key = JSON.get(row, "prop");
			boolean value = (Boolean) JSON.get(row, "enabled");

			tmp.put(key, value);
		}
		JSON.put(backingStore, "debuffs", tmp);

		tmp = new HashMap<String,Object>();
		for (Map<String,Object> row : (List<Map<String,Object>>) buffTable.getTableData()) {
			String key = JSON.get(row, "prop");
			boolean value = (Boolean) JSON.get(row, "enabled");

			tmp.put(key, value);
		}
		JSON.put(backingStore, "buffs", tmp);
	}
}
